<template>
  <div class="login">
    <div class="login-container">
      <div class="avatar-img">
        <img src="../../assets/logo.png" alt="" />
      </div>
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="用户名" prop="username">
          <el-input
            prefix-icon="el-icon-s-custom"
            v-model.number="ruleForm.username"
            placeholder="5-14位"
          ></el-input>
        </el-form-item>

        <el-form-item label="密码" prop="password">
          <el-input
            type="password"
            v-model="ruleForm.password"
            prefix-icon="el-icon-lock"
            autocomplete="off"
            placeholder="6-16位"
          ></el-input>
        </el-form-item>

        <el-form-item class="btn">
          <el-button @click="resetForm('ruleForm')" type="info">重置</el-button>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >登录</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    var checkName = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('用户名不能为空'))
      }
      setTimeout(() => {
        var reg = /^\w{5,14}$/

        if (!reg.test(value)) {
          callback(new Error('账号不符合要求'))
        } else {
          callback()
        }
      }, 200) 
    }
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      } else {
        var reg = /^\w{6,16}$/
        // if (this.ruleForm.checkPass !== "") {
        //   this.$refs.ruleForm.validateField("checkPass");
        // }
        if (reg.test(value)) {
          callback()
        } else {
          callback(new Error('密码不符合要求'))
        }
      }
    }
    // var validatePass2 = (rule, value, callback) => {
    //   if (value === "") {
    //     callback(new Error("请再次输入密码"));
    //   } else if (value !== this.ruleForm.pass) {
    //     callback(new Error("两次输入密码不一致!"));
    //   } else {
    //     callback();
    //   }
    // };
    return {
      ruleForm: {
        username: 'admin',
        password: '123456',
        // checkPass: "",
      },
      rules: {
        username: [{ validator: checkName, trigger: 'blur' }],
        password: [{ validator: validatePass, trigger: 'blur' }],
        // checkPass: [{ validator: validatePass2, trigger: "blur" }],
      },
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$store
            .dispatch('loginIn', this.ruleForm)
            .then(() => {
              this.showToast(true, '登录成功')
              this.$router.push('/home')
            })
            .catch(() => {
              this.showToast(false, '登录失败')
            })
        } else {
          console.log('error submit!!')
          this.showToast(false, '验证失败')
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    showToast(status, msg) {
      this.$message({
        type: status ? 'success' : 'error',
        message: msg,
      })
    },
  },
}
</script>

<style scoped lang="less">
@import './login.less';
</style>
